package com.GlobalPaint.app.ui.location;


import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.GlobalPaint.app.Adapter.MyFriendsAdapter;
import com.GlobalPaint.app.Base.DialogWaiting;
import com.GlobalPaint.app.R;
import com.GlobalPaint.app.bean.MyFriendBean;
import com.GlobalPaint.app.bean.ReturnStatusResultEntity;
import com.GlobalPaint.app.utils.Constants;
import com.GlobalPaint.app.utils.DataManager;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;


public class MyFriendsFragment extends Fragment {
    private MyFriendsAdapter adapter;
    private MyFriendBean bean;
    private List<MyFriendBean.DataBean> list;
    private LinearLayoutManager linearLayoutManager;
    private XRecyclerView xrecycler;
    private int page;
    private Dialog dlgWaiting2;
    private LocalReceiver localReceiver;
    private IntentFilter intentFilter;

    private LocalBroadcastManager localBroadcastManager;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_my_friends, container, false);
        initView(view);
        localBroadcastManager = LocalBroadcastManager.getInstance(getActivity());
        intentFilter=new IntentFilter();
        intentFilter.addAction("shuaxinhaoyou");
        localReceiver=new LocalReceiver();

        localBroadcastManager.registerReceiver(localReceiver, intentFilter);
        return view;
    }

    private void initView(View view) {
        xrecycler = (XRecyclerView)view.findViewById(R.id.xrecycler);
        linearLayoutManager = new LinearLayoutManager(getContext());
        xrecycler.setLayoutManager(linearLayoutManager);
        xrecycler.setLoadingMoreEnabled(false);
        getFriends();
        initLoad();
    }
        public void job(){
            String url= Constants.JASON_SERVICE_URL+"appFriends/getFriends?userName="+ DataManager.userEntity.getUserName();
            DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), MyFriendBean.class, new DataManager.Callback() {
                @Override
                public void onSuccess(Object o) {

                    bean=(MyFriendBean)o;
                    if (bean.getStatus() == 1){
                        if(bean.getData() != null && bean.getData().size()>0){
                            list=bean.getData();
                            adapter=new MyFriendsAdapter(list, MyFriendsFragment.this);
                            xrecycler.setAdapter(adapter);
                            adapter.notifyDataSetChanged();
                        }else {
                            list=new ArrayList<MyFriendBean.DataBean>();
                            adapter=new MyFriendsAdapter(list, MyFriendsFragment.this);
                            xrecycler.setAdapter(adapter);
                            adapter.notifyDataSetChanged();
                        }
                    }
                    else {
                        Toast.makeText(getContext(),bean.getMsg(),Toast.LENGTH_LONG).show();
                    }
                    xrecycler.refreshComplete();
                }

                @Override
                public void onFailure(ReturnStatusResultEntity error) {
                    if(bean !=null){
                        if(bean.getData() != null && bean.getData().size()>0) {
                            bean.getData().clear();
                            Toast.makeText(getContext(), bean.getMsg(), Toast.LENGTH_LONG).show();
                            adapter.notifyDataSetChanged();
                            xrecycler.refreshComplete();
                        }
                    }

                }
            });
        }
    private void getFriends() {
        dlgWaiting2 = DialogWaiting.createDialogWaiting(getContext(), "加载中......");
        dlgWaiting2.show();
        String url= Constants.JASON_SERVICE_URL+"appFriends/getFriends?userName="+ DataManager.userEntity.getUserName();
        DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), MyFriendBean.class, new DataManager.Callback() {
            @Override
            public void onSuccess(Object o) {
                dlgWaiting2.cancel();
                bean=(MyFriendBean)o;
                if (bean.getStatus() == 1){
                    if(bean.getData() != null && bean.getData().size()>0){
                        list=bean.getData();
                        adapter=new MyFriendsAdapter(list,MyFriendsFragment.this);
                        xrecycler.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }else {
                        list=new ArrayList<MyFriendBean.DataBean>();
                        adapter=new MyFriendsAdapter(list, MyFriendsFragment.this);
                        xrecycler.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }
                }
                else {
                    Toast.makeText(getContext(),bean.getMsg(),Toast.LENGTH_LONG).show();
                }
                xrecycler.refreshComplete();
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {
                dlgWaiting2.cancel();
                if(bean !=null){
                    if(bean.getData() != null && bean.getData().size()>0) {
                        bean.getData().clear();
                        Toast.makeText(getContext(), bean.getMsg(), Toast.LENGTH_LONG).show();
                        adapter.notifyDataSetChanged();
                        xrecycler.refreshComplete();
                    }
                }

            }
        });
        dlgWaiting2.cancel();
    }
    public  void deleteUser(final String id,final int position) {
        String url= Constants.JASON_SERVICE_URL+"appFriends/cancelAttention?userName="+
                DataManager.userEntity.getUserName()+"&jid="+id;
        DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), MyFriendBean.class, new DataManager.Callback() {
            @Override
            public void onSuccess(Object o) {
                bean=(MyFriendBean)o;
                if(bean.getStatus()==1){
                    list.remove(position);
                    adapter.notifyDataSetChanged();
                }
                else {
                    Toast.makeText(getContext(),bean.getMsg(),Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {


            }
        });
    }
    private void initLoad() {
        xrecycler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                String url= Constants.JASON_SERVICE_URL+"appFriends/getFriends?userName="+ DataManager.userEntity.getUserName();
                DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), MyFriendBean.class, new DataManager.Callback() {
                    @Override
                    public void onSuccess(Object o) {
                        page = 1;
                        MyFriendBean talentBean=(MyFriendBean)o;
                        if( bean.getStatus() == 1){
                            if (bean.getData() != null && bean.getData().size()>0){
                                bean.getData().clear();
                                bean.getData().addAll(talentBean.getData());
                                adapter.notifyDataSetChanged();
                                xrecycler.loadMoreComplete();
                            }
                        }
                        else {
                            Toast.makeText(getContext(),bean.getMsg(),Toast.LENGTH_LONG).show();
                        }
                        xrecycler.refreshComplete();
                    }

                    @Override
                    public void onFailure(ReturnStatusResultEntity error) {
                        xrecycler.refreshComplete();
                    }
                });
            }

            @Override
            public void onLoadMore() {

            }
        });
    }
    //接受广播 回调mess页面
    class LocalReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

            //Toast.makeText(context,"dddddd",Toast.LENGTH_LONG).show();
            job();
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        localBroadcastManager.unregisterReceiver(localReceiver);
    }
}
